﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Configuration;
using System.IO;
using ProcessControl.Entity;

namespace ProcessControl.Logic
{
    public class AttachmentLogic
    {
        private DataHelper db = new DataHelper();
        public string flag = DataHelper.ParameterFlag;
        public DataTable GetAttachmentsByProcessAndIncident(string ProcessName, int Incident)
        {
            try
            {
                StringBuilder strsql = new StringBuilder();
                strsql.Append("select * from WF_Attachment where ProcessName='" + ProcessName + "' and Incident=" + Incident.ToString());
                DbCommand dbcom = db.GetSqlStringCommond(strsql.ToString());
                //db.AddInParameter(dbcom, flag+"ProcessName", DbType.String, ProcessName);
                //db.AddInParameter(dbcom, flag+"Incident", DbType.Int32, Incident);
                return db.ExecuteDataTable(dbcom);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable GetAttachmentsByFormID(string strFormid)
        {
            try
            {
                StringBuilder strsql = new StringBuilder();
                strsql.Append("select * from WF_Attachment where EXT01='" + strFormid + "'");
                DbCommand dbcom = db.GetSqlStringCommond(strsql.ToString());
                //db.AddInParameter(dbcom, flag+"ProcessName", DbType.String, ProcessName);
                //db.AddInParameter(dbcom, flag+"Incident", DbType.Int32, Incident);
                return db.ExecuteDataTable(dbcom);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Albert add按流程步骤名和FormID查询
        public DataTable GetAttachmentsByFormIDAndStepName(string strFormid, string StepName)
        {
            try
            {
                StringBuilder strsql = new StringBuilder();
                strsql.Append("select * from WF_Attachment where EXT01='" + strFormid + "' and UPLOADSTEPNAME='" + StepName + "'");
                DbCommand dbcom = db.GetSqlStringCommond(strsql.ToString());
                //db.AddInParameter(dbcom, flag+"ProcessName", DbType.String, ProcessName);
                //db.AddInParameter(dbcom, flag+"Incident", DbType.Int32, Incident);
                return db.ExecuteDataTable(dbcom);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<AttachmentEntity> GetAttachmentsByProc(string ProcessName, int Incident)
        {
            try
            {
                StringBuilder strsql = new StringBuilder();
                strsql.Append("select * from WF_Attachment where ProcessName=" + flag + "ProcessName and Incident=" + flag + "Incident");
                DbCommand dbcom = db.GetSqlStringCommond(strsql.ToString());
                db.AddInParameter(dbcom, flag + "ProcessName", DbType.String, ProcessName);
                db.AddInParameter(dbcom, flag + "Incident", DbType.Int32, Incident);
                DbDataReader dr = db.ExecuteReader(dbcom);
                List<AttachmentEntity> list = new List<AttachmentEntity>();
                while (dr.Read())
                {
                    AttachmentEntity item = new AttachmentEntity();
                    if (dr["ID"] != null && !String.IsNullOrEmpty(dr["ID"].ToString()))
                    {
                        item.ID = dr["ID"].ToString();
                    }
                    if (dr["ProcessName"] != null && !String.IsNullOrEmpty(dr["ProcessName"].ToString()))
                    {
                        item.ProcessName = dr["ProcessName"].ToString();
                    }
                    if (dr["Incident"] != null && !String.IsNullOrEmpty(dr["Incident"].ToString()))
                    {
                        try
                        {
                            item.Incident = Convert.ToInt32(dr["Incident"].ToString());
                        }
                        catch
                        {
                            item.Incident = 0;
                        }
                    }
                    if (dr["UploadStepName"] != null && !String.IsNullOrEmpty(dr["UploadStepName"].ToString()))
                    {
                        item.UploadStepName = dr["UploadStepName"].ToString();
                    }
                    if (dr["FileName"] != null && !String.IsNullOrEmpty(dr["FileName"].ToString()))
                    {
                        item.FileName = dr["FileName"].ToString();
                    }
                    if (dr["NewName"] != null && !String.IsNullOrEmpty(dr["NewName"].ToString()))
                    {
                        item.NewName = dr["NewName"].ToString();
                    }
                    if (dr["FileSize"] != null && !String.IsNullOrEmpty(dr["FileSize"].ToString()))
                    {
                        try
                        {
                            item.FileSize = Convert.ToInt32(dr["FileSize"].ToString());
                        }
                        catch
                        {
                            item.FileSize = 0;
                        }
                    }
                    if (dr["FileType"] != null && !String.IsNullOrEmpty(dr["FileType"].ToString()))
                    {
                        item.FileType = dr["FileType"].ToString();
                    }
                    if (dr["Comments"] != null && !String.IsNullOrEmpty(dr["Comments"].ToString()))
                    {
                        item.Comments = dr["Comments"].ToString();
                    }
                    if (dr["CreateByBadge"] != null && !String.IsNullOrEmpty(dr["CreateByBadge"].ToString()))
                    {
                        item.CreateByBadge = dr["CreateByBadge"].ToString();
                    }
                    if (dr["CreateByName"] != null && !String.IsNullOrEmpty(dr["CreateByName"].ToString()))
                    {
                        item.CreateByName = dr["CreateByName"].ToString();
                    }
                    if (dr["CreateByDomain"] != null && !String.IsNullOrEmpty(dr["CreateByDomain"].ToString()))
                    {
                        item.CreateByDomain = dr["CreateByDomain"].ToString();
                    }
                    if (dr["CreateDate"] != null && !String.IsNullOrEmpty(dr["CreateDate"].ToString()))
                    {
                        try
                        {
                            item.CreateDate = Convert.ToDateTime(dr["CreateDate"].ToString());
                        }
                        catch
                        { }
                    }
                    if (dr["Status"] != null && !String.IsNullOrEmpty(dr["Status"].ToString()))
                    {
                        item.Status = dr["Status"].ToString();
                    }
                    if (dr["Ext01"] != null && !String.IsNullOrEmpty(dr["Ext01"].ToString()))
                    {
                        item.Ext01 = dr["Ext01"].ToString();
                    }
                    if (dr["Ext02"] != null && !String.IsNullOrEmpty(dr["Ext02"].ToString()))
                    {
                        item.Ext02 = dr["Ext02"].ToString();
                    }
                    if (dr["Ext03"] != null && !String.IsNullOrEmpty(dr["Ext03"].ToString()))
                    {
                        item.Ext03 = dr["Ext03"].ToString();
                    }
                    if (dr["Ext04"] != null && !String.IsNullOrEmpty(dr["Ext04"].ToString()))
                    {
                        item.Ext04 = dr["Ext04"].ToString();
                    }
                    if (dr["Ext05"] != null && !String.IsNullOrEmpty(dr["Ext05"].ToString()))
                    {
                        item.Ext05 = dr["Ext05"].ToString();
                    }
                    list.Add(item);
                }
                return new List<AttachmentEntity>();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public AttachmentEntity GetAttachmentsByID(string ID)
        {
            try
            {
                StringBuilder strsql = new StringBuilder();
                strsql.Append("select * from WF_Attachment where NEWNAME=" + flag + "ID");
                DbCommand dbcom = db.GetSqlStringCommond(strsql.ToString());
                db.AddInParameter(dbcom, flag + "ID", DbType.String, ID);
                DbDataReader dr = db.ExecuteReader(dbcom);
                AttachmentEntity item = new AttachmentEntity();
                while (dr.Read())
                {
                    //if (dr["ID"] != null && !String.IsNullOrEmpty(dr["ID"].ToString()))
                    //{
                    //    item.ID = dr["ID"].ToString();
                    //}
                    if (dr["ProcessName"] != null && !String.IsNullOrEmpty(dr["ProcessName"].ToString()))
                    {
                        item.ProcessName = dr["ProcessName"].ToString();
                    }
                    if (dr["Incident"] != null && !String.IsNullOrEmpty(dr["Incident"].ToString()))
                    {
                        try
                        {
                            item.Incident = Convert.ToInt32(dr["Incident"].ToString());
                        }
                        catch
                        {
                            item.Incident = 0;
                        }
                    }
                    if (dr["UploadStepName"] != null && !String.IsNullOrEmpty(dr["UploadStepName"].ToString()))
                    {
                        item.UploadStepName = dr["UploadStepName"].ToString();
                    }
                    if (dr["FileName"] != null && !String.IsNullOrEmpty(dr["FileName"].ToString()))
                    {
                        item.FileName = dr["FileName"].ToString();
                    }
                    if (dr["NewName"] != null && !String.IsNullOrEmpty(dr["NewName"].ToString()))
                    {
                        item.NewName = dr["NewName"].ToString();
                    }
                    if (dr["FileSize"] != null && !String.IsNullOrEmpty(dr["FileSize"].ToString()))
                    {
                        try
                        {
                            item.FileSize = Convert.ToInt32(dr["FileSize"].ToString());
                        }
                        catch
                        {
                            item.FileSize = 0;
                        }
                    }
                    if (dr["FileType"] != null && !String.IsNullOrEmpty(dr["FileType"].ToString()))
                    {
                        item.FileType = dr["FileType"].ToString();
                    }
                    if (dr["Comments"] != null && !String.IsNullOrEmpty(dr["Comments"].ToString()))
                    {
                        item.Comments = dr["Comments"].ToString();
                    }
                    if (dr["CreateByBadge"] != null && !String.IsNullOrEmpty(dr["CreateByBadge"].ToString()))
                    {
                        item.CreateByBadge = dr["CreateByBadge"].ToString();
                    }
                    if (dr["CreateByName"] != null && !String.IsNullOrEmpty(dr["CreateByName"].ToString()))
                    {
                        item.CreateByName = dr["CreateByName"].ToString();
                    }
                    if (dr["CreateByDomain"] != null && !String.IsNullOrEmpty(dr["CreateByDomain"].ToString()))
                    {
                        item.CreateByDomain = dr["CreateByDomain"].ToString();
                    }
                    if (dr["CreateDate"] != null && !String.IsNullOrEmpty(dr["CreateDate"].ToString()))
                    {
                        try
                        {
                            item.CreateDate = Convert.ToDateTime(dr["CreateDate"].ToString());
                        }
                        catch
                        { }
                    }
                    if (dr["Status"] != null && !String.IsNullOrEmpty(dr["Status"].ToString()))
                    {
                        item.Status = dr["Status"].ToString();
                    }
                    if (dr["Ext01"] != null && !String.IsNullOrEmpty(dr["Ext01"].ToString()))
                    {
                        item.Ext01 = dr["Ext01"].ToString();
                    }
                    if (dr["Ext02"] != null && !String.IsNullOrEmpty(dr["Ext02"].ToString()))
                    {
                        item.Ext02 = dr["Ext02"].ToString();
                    }
                    if (dr["Ext03"] != null && !String.IsNullOrEmpty(dr["Ext03"].ToString()))
                    {
                        item.Ext03 = dr["Ext03"].ToString();
                    }
                    if (dr["Ext04"] != null && !String.IsNullOrEmpty(dr["Ext04"].ToString()))
                    {
                        item.Ext04 = dr["Ext04"].ToString();
                    }
                    if (dr["Ext05"] != null && !String.IsNullOrEmpty(dr["Ext05"].ToString()))
                    {
                        item.Ext05 = dr["Ext05"].ToString();
                    }
                }
                return item;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool DeleteAttachmentsByID(string ID)
        {
            try
            {
                AttachmentEntity item = GetAttachmentsByID(ID);
                string path = HttpContext.Current.Server.MapPath(MyLib.ConfigurationManager.AppSettings["AttachmentPath"] + "/" + item.ProcessName + "/" + item.NewName + item.FileType);
                File.Delete(path);
                StringBuilder strsql = new StringBuilder();
                if (ID.IndexOf(",") < 0)
                    strsql.Append("delete from WF_Attachment where NEWNAME='" + ID + "'");
                else
                    strsql.Append("delete from WF_Attachment where NEWNAME in (" + ID + ")");
                if (db.ExecuteNonQuery(strsql.ToString()) > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DownloadByID(System.Web.UI.Page page, string ID)
        {
            try
            {
                string encoding = "gb2312";
                AttachmentEntity item = GetAttachmentsByID(ID);
                string filePath = HttpContext.Current.Server.MapPath(MyLib.ConfigurationManager.AppSettings["AttachmentPath"] + "/" + item.ProcessName + "/" + item.NewName + item.FileType);
                FileInfo fileInfo = new FileInfo(filePath);
                string fileName = item.FileName;
                fileName = HttpUtility.UrlEncode(fileName);
                page.Response.Clear();
                page.Response.ClearContent();
                page.Response.ClearHeaders();
                System.Web.HttpContext.Current.Response.Charset = encoding;
                page.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                page.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                page.Response.AddHeader("Content-Transfer-Encoding", "binary");
                page.Response.ContentType = "application/octet-stream";
                System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(encoding);
                System.Web.HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.GetEncoding(encoding);
                //page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                page.Response.WriteFile(fileInfo.FullName);
                page.Response.Flush();
                page.Response.End();
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }

        public bool UploadFile(AttachmentEntity item)
        {
            try
            {
                StringBuilder strsql = new StringBuilder();
                strsql.Append("INSERT INTO WF_Attachment(ProcessName,Incident,UploadStepName,FileName,NewName,FileSize,FileType,Comments,CreateByBadge,CreateByName,CreateByDomain,CreateDate,Status,Ext01,Ext02,Ext03,Ext04,Ext05)");
                strsql.Append(" values ");
                strsql.Append("(" + flag + "ProcessName," + flag + "Incident," + flag + "UploadStepName," + flag + "FileName," + flag + "NewName," + flag + "FileSize," + flag + "FileType," + flag + "Comments," + flag + "CreateByBadge," + flag + "CreateByName," + flag + "CreateByDomain," + flag + "CreateDate," + flag + "Status," + flag + "Ext01," + flag + "Ext02," + flag + "Ext03," + flag + "Ext04," + flag + "Ext05)");
                DbCommand dbcom = db.GetSqlStringCommond(strsql.ToString());
                db.AddInParameter(dbcom, flag + "ProcessName", DbType.String, item.ProcessName);
                db.AddInParameter(dbcom, flag + "Incident", DbType.Int32, item.Incident);
                db.AddInParameter(dbcom, flag + "UploadStepName", DbType.String, item.UploadStepName);
                db.AddInParameter(dbcom, flag + "FileName", DbType.String, item.FileName);
                db.AddInParameter(dbcom, flag + "NewName", DbType.String, item.NewName);
                db.AddInParameter(dbcom, flag + "FileSize", DbType.Decimal, item.FileSize);
                db.AddInParameter(dbcom, flag + "FileType", DbType.String, item.FileType);
                db.AddInParameter(dbcom, flag + "Comments", DbType.String, item.Comments);
                db.AddInParameter(dbcom, flag + "CreateByBadge", DbType.String, item.CreateByBadge);
                db.AddInParameter(dbcom, flag + "CreateByName", DbType.String, item.CreateByName);
                db.AddInParameter(dbcom, flag + "CreateByDomain", DbType.String, item.CreateByDomain);
                db.AddInParameter(dbcom, flag + "CreateDate", DbType.DateTime, item.CreateDate);
                db.AddInParameter(dbcom, flag + "Status", DbType.String, item.Status);
                db.AddInParameter(dbcom, flag + "Ext01", DbType.String, item.Ext01);
                db.AddInParameter(dbcom, flag + "Ext02", DbType.String, item.Ext02);
                db.AddInParameter(dbcom, flag + "Ext03", DbType.String, item.Ext03);
                db.AddInParameter(dbcom, flag + "Ext04", DbType.String, item.Ext04);
                db.AddInParameter(dbcom, flag + "Ext05", DbType.String, item.Ext05);

                foreach (DbParameter param in dbcom.Parameters)
                {
                    if (param.Value == null)
                    {
                        param.Value = DBNull.Value;
                    }
                }
                if (db.ExecuteNonQuery(dbcom) > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}